import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import TableControls from '#cpt/temp/TableControls'
import Confirm from '#cpt/temp/Confirm'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
const ControlModal 			= $Lazy.state(()=>import('./cpt/control-modal'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		// 搜索表单列表
		forms : [
			{ label: '条码号',		name: 'spec_code' },
			{ label: '报告单编号',	name: 'report_num' },
			{ label: '合并规则',		name: 'merge_group_id',	type: 'select', loading:true },
		]
	}
	
	model = {}
	
	componentDidMount(){
		
		this.getSelect()
		
		if(window.isInit){
			this.fetch()
		}
	}
	// 获取下拉数据
	getSelect = async () => {
		const { forms } = this.state
		$cache.then( async f => {
			// 合并规则
			$fn.setRow(forms, 'merge_group_id', await f.getCache({ api: 'merge_select' } ) )
			//
			this.setState({ forms })
		})
		
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'merge-report/index')
	
	// 表单
	cols = [
		{ title: '条码号', 		dataIndex: 'spec_code', 		...$fn.table.spec_code },
		{ title: '报告单编号', 	dataIndex: 'report_num', 		width:150 },
		{ title: '报告单模板', 	dataIndex: 'report_name', 		width:100 },
		{ title: '姓名', 		dataIndex: 'patient_name', 		...$fn.table.name },
		{ title: '规则名称', 	dataIndex: 'merge_name', 		width:150 },
		{ title: '处理方法', 	dataIndex: 'handle_type_name', 	width:100 },
		{ title: '创建时间', 	dataIndex: 'created_at', 		...$fn.table.time },
		TableControls.controls.call(this,{width:250})
	]
	// 表格操作按钮
	table_controls = (text, { report_num, uuid, spec_code } ) => {
		return [
			TableControls.pushTo.call(this, '预览报告单', `preview-report?spec_num=${report_num}&spec_code=${spec_code}`),
			Confirm.call(this, '召回报告单', 'merge-report/stop', { query: { report_num } }),															// 召回报告单
			TableControls.click.call(this, '查看项目明细', ()=> this.modal.open(uuid) )
		]
	}
	render(){
		return (
			<>
				<Page title='合并项目列表'>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this,{isInit:window.isInit}) }
				</Page>
				{/* 查看项目明细弹窗 */}
				<ControlModal onRef={ref=>this.modal=ref} />
			</>
		)
	}
}

export default Index